import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class ThemePreview extends StatefulWidget {
  final ThemeData theme;

  const ThemePreview({super.key, required this.theme});

  @override
  State<ThemePreview> createState() => _ThemePreviewState();
}

class _ThemePreviewState extends State<ThemePreview>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scale;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 350),
    );
    _scale = Tween<double>(begin: 1.0, end: 1.02).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
    );
  }

  @override
  void didUpdateWidget(covariant ThemePreview oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 当主题变化时触发呼吸缩放
    if (oldWidget.theme.brightness != widget.theme.brightness) {
      _controller.forward(from: 0).then((_) => _controller.reverse());
    }
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    return AnimatedBuilder(
      animation: _scale,
      builder: (context, child) {
        return Transform.scale(
          scale: _scale.value,
          child: AnimatedTheme(
            data: widget.theme,
            duration: const Duration(milliseconds: 300),
            curve: Curves.easeInOut,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(16),
              child: Material(
                elevation: 3,
                child: Column(
                  children: [
                    AppBar(
                      title: Text(l10n.themeModePreviewTitle),
                      centerTitle: true,
                      automaticallyImplyLeading: false,
                    ),
                    Expanded(
                      child: ListView(
                        padding: const EdgeInsets.all(12),
                        children: [
                          Card(
                            child: ListTile(
                              leading: const Icon(Icons.star),
                              title: Text(l10n.themeModeSampleCard),
                              subtitle: Text(l10n.themeModeSampleCardSubtitle),
                              trailing:
                                  const Icon(Icons.arrow_forward_ios, size: 14),
                            ),
                          ),
                          const SizedBox(height: 8),
                          FilledButton.icon(
                            onPressed: () {},
                            icon: const Icon(Icons.send),
                            label: Text(l10n.themeModeSampleButton),
                          ),
                        ],
                      ),
                    ),
                    NavigationBar(
                      destinations: [
                        NavigationDestination(
                            icon: Icon(Icons.home),
                            label: l10n.themeModeTabHome),
                        NavigationDestination(
                            icon: Icon(Icons.search),
                            label: l10n.themeModeTabSearch),
                        NavigationDestination(
                            icon: Icon(Icons.person),
                            label: l10n.themeModeTabMine),
                      ],
                      selectedIndex: 0,
                    )
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
